package com.talent.jiwen.http.common;

import android.app.Activity;
import android.content.Intent;

import com.talent.jiwen.ActivityManager;
import com.talent.jiwen.base.App;
import com.talent.jiwen.http.result.HomePageResult;
import com.talent.jiwen.my.PayActivity;
import com.talent.jiwen.util.Constant;
import com.talent.jiwen.util.MaterialDialogUtil;
import com.talent.jiwen.util.MyToast;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import rx.Observable;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * @author cmw
 * @date 创建时间：2017/8/5
 * @description
 */

public class ApiException extends RuntimeException {

    private static String message;

    public ApiException(int resultCode) {
        this(getApiExceptionMessage(resultCode));
    }

    public ApiException(String detailMessage) {
        super(detailMessage);
    }

    @Override
    public String getMessage() {
        return message;
    }

    /**
     * 由于服务器传递过来的错误信息直接给用户看的话，用户未必能够理解
     * 需要根据错误码对错误信息进行一个转换，在显示给用户
     *
     * @param code
     * @return
     */
    private static String getApiExceptionMessage(int code) {
        switch (code) {
            case 900001:
                message = "手机号码已被注册";
                break;
            case 900002:
                message = "验证码发送失败";
                break;
            case 900003:
                message = "用户注册失败";
                break;
            case 900004:
                message = "用户不存在";
                break;
            case 900005:
                message = "密码错误";
                break;
            case 900006:
                message = "账号异常";
                break;
            case 900007:
                message = "用户已存在";
                break;
            case 900009:
                message = "获取个人信息失败";
                break;
            case 900010:
                message = "信息修改失败";
                break;
            case 900011:
                message = "密码修改失败";
                break;
            case 900012:
                message = "列表获取失败";
                break;
            case 900013:
                message = "目标新建失败";
                break;
            case 900014:
                message = "历史目标详细信息获取失败";
                break;
            case 900015:
                message = "目标审核失败";
                break;
            case 900016:
                message = "新建综合评价失败";
                break;
            case 900017:
                message = "综合评价详情获取失败";
                break;
            case 900018:
                message = "评价失败";
                break;
            case 900019:
                message = "手机号码修改失败";
                break;
            case 900020:
                message = "密码重置失败";
                break;
            case 900021:
                message = "选择科目失败";
                break;
            case 900022:
                message = "作业添加失败";
                break;
            case 900023:
                message = "作业发布失败";
                break;
            case 900024:
                message = "作业详情获取失败";
                break;
            case 900025:
                message = "作业提交失败";
                break;
            case 900026:
                message = "保存作业批改结果失败";
                break;
            case 900027:
                message = "作业督促失败";
                break;
            case 900028:
                message = "消息详情获取失败";
                break;
            case 900029:
                message = "题目详情获取失败";
                break;
            case 900030:
                message = "获取作业批改结果失败";
                break;
            case 900031:
                message = "作业结果公布失败";
                break;
            case 900032:
                message = "新建综合评价提示失败";
                break;
            case 900033:
                message = "删除作业失败";
                break;
            case 900034:
                message = "删除作业失败";
                break;
            case 900035:
                message = "所选科目暂无任课教师";
                break;
            case 900201:
                message = "接口内部错误";
                break;
            case 900301:
                message = "信息获取异常";
                break;
            case 900036:
                message = "时间耗尽，作业自动提交异常";
                break;
            case 900037:
                message = "版本信息获取失败";
                break;
            case 900038:
                message = "用户登录失败";
                break;
            case 900039:
                message = "该手机号未注册!";
                break;
            case 900999:
                message = "短信发送频繁,请稍后重试";
                break;
            case 900702:
                message = "你们已经不是好友了";
                break;
            case 900401:
                message = "账号异常";
                toLogin();
                break;
            case 900501:
                message = "账号异常";
                toLogin();
                break;
            case 999998:
                message = "此账号在其他地方登录";
                toLogin();
                break;
            case 999999:
                message = "请重新登录";
                toLogin();
                break;
            case 900008:
                message = "暂无数据";
                break;
            case 900600:
                message = "积分不足";
                break;
            case 900721:
                message = "创建失败";
                break;
            case 903001:
                message = "创建聊天室失败";
                break;
            case 903002:
                message = "创建订单失败";
                break;
            case 903003:
                message = "数据获取失败";
                break;
            case 903004:
//                message = "创建群组失败";
                message = "您当前有正在进行中的即时订单，请先处理";
                break;
            case 903005:
                message = "邀请学生加入群组失败";
                break;
            case 903006:
                message = "邀请已失效";
                break;
            case 903007:
                message = "收藏教师失败";
                break;
            case 903008:
                message = "抢单失败";
                break;
            case 903009:
                message = "认证失败";
                break;
            case 903010:
                message = "发布订单失败";
                break;
            case 903011:
                message = "接受教师抢单失败";
                break;
            case 903012:
                message = "开始上课失败";
                break;
            case 903013:
                message = "结束上课失败";
                break;
            case 903014:
                message = "评价订单失败";
                break;
            case 903015:
                message = "保存学生评分失败";
                break;
            case 903016:
                message = "群组成员已满";
                break;
            case 903017:
                message = "暂未通过认证或没有认证";
                break;
            case 903018:
                message = "更新上课时间失败";
                break;
            case 903019:
                message = "取消收藏教师失败";
                break;
            case 903020:
                message = "更新个人信息失败";
                break;
            case 903021:
                message = "取消订单失败";
                break;
            case 903022:
                message = "初始化微信支付失败";
                break;
            case 903023:
                message = "获取首页信息失败";
                break;
            case 903024:
                message = "取消订单失败";
                break;
            case 903025:
                message = "用户取消订单达到最大值,每天只能取消一次";
                break;
            case 903026:
                message = "取消订单失败，距离上课时间小于十分钟";
                break;
            case 903027:
                message = "加入课程失败";
                break;
            case 903028:
                message = "用户不存在";
                break;
            case 903029:
                message = "账户已被注销";
                break;
            case 903030:
                message = "您暂时处于失信状态,请先缴纳保证金";
                toPay();
                break;
            case 903031:
                message = "即时单无法取消";
                break;
            case 903032:
                message = "更新个人介绍失败";
                break;
            case 903033:
                message = "回复学生评论失败";
                break;
            case 903034:
                message = "回复学生评论失败，已评价";
                break;
            case 903035:
                message = "支付宝支付启动失败";
                break;
            case 903036:
                message = "订单已支付";
                break;
            case 903037:
                message = "订单未支付";
                toOrderPay();
                break;
            case 903038:
                message = "用户保存申诉信息失败";
                break;
            case 903039:
                message = "订单不存在";
                break;
            case 903040:
                message = "教师不存在";
                break;
            case 903041:
                message = "用户提现失败";
                break;
            case 903042:
                message = "大于可提现资金";
                break;
            case 903043:
                message = "登录失败，密码错误";
                break;
            case 903044:
                message = "登录失败";
                break;
            case 903045:
                message = "验证码已失效";
                break;
            case 903046:
                message = "用户已存在";
                break;
            case 903047:
                message = "用户注册失败";
                break;
            case 903048:
                message = "用户重置密码失败";
                break;
            case 903049:
                message = "账号已被注销";
                break;
            case 903050:
                message = "手机号已被注册";
                break;
            case 903051:
                message = "验证码发送失败";
                break;
            case 903052:
                message = "手机号未被注册";
                break;
            case 903053:
                message = "申诉失败，超过最晚申诉时间";
                break;
            case 903054:
                message = "支付宝签名失败";
                break;
            case 903055:
                message = "用户绑定支付宝失败";
                break;
            case 903056:
                message = "取用户绑定支付宝状态失败";
                break;
            case 903057:
                message = "学生存在未支付的订单";
                break;
            case 903058:
                message = "请先绑定支付宝账号";
                break;
            case 903059:
                message = "缴纳保证金失败";
                break;
            case 903060:
                message = "你未处于失信状态";
                break;
            case 903061:
                message = "你处于失信状态,请联系客服专员解决";
                break;
            case 901001:
                message = "验证码错误或验证码已过期!";
                break;
            case 903062:
                message = "提现小于最小提现金额,请大于0.1元";
                break;
            case 903063:
                message = "存在即时单";
                break;
            case 903064:
                message = "订单已取消";
                break;
            case 903065:
                message = "申请已失效";
                break;
            case 903066:
                message = "申请已被学生采纳";
                break;
            case 903067:
                message = "已存在预约单";
                break;
            case 903072:
                message = "教师更新收费失败";
                break;
            case 903073:
                message = "用户处于冻结状态";
                break;
            case 903074:
                message = "更新身份证号失败";
                break;
            case 903075:
                message = "身份证号已被绑定";
                break;
            case 903076:
                message = "错误的身份证号";
                break;
            case 903077:
                message = "年龄不在优惠区间";
                break;
            case 903078:
                message = "学生提现失败（用户存在进行中的订单）";
                break;
            case 903079:
                message = "初始化充值失败";
                break;
            case 903080:
                message = "初始化余额支付失败";
                break;
            case 903081:
                message = "学生优惠券已耗尽";
                break;
            case 903082:
                message = "学生优惠券已耗尽";
                break;
            case 903083:
                message = "教师申请教学补贴失败";
                break;
            default:
                message = "服务器异常，请稍后重试";
                break;
        }
        return message;
    }


    private static void toLogin(){

        JPushInterface.deleteAlias(App.getInstance() , (int)Math.random());
        Intent intent1=new Intent(App.getInstance(), com.talent.jiwen.LoginActivity.class);
        intent1.addFlags(FLAG_ACTIVITY_NEW_TASK);
        SPUtil.clearAll();
        ActivityManager.getAppManager().finishAllActivity();
        Constant.getContext().startActivity(intent1);

    }

    private static void toPay(){//缴纳保证金
        Intent intent1=new Intent(App.getInstance(), com.talent.jiwen.my.PayDepositActivity.class);
        intent1.addFlags(FLAG_ACTIVITY_NEW_TASK);
        Constant.getContext().startActivity(intent1);

    }

    /**订单未支付
     *
     */
    private static void toOrderPay(){
        final Activity activity = ActivityManager.getAppManager().currentActivity();
        if (null != activity){
            MaterialDialogUtil.getCommonDialog(activity, "提示", "您当前有未支付订单,是否立即去支付"
                    , "确定", "取消"
                    , new MaterialDialogUtil.OnDialogPoNeClickListener() {
                        @Override
                        public void onPositive() {
                            getUnPaidOrder(activity);
                        }

                        @Override
                        public void onNegative() {

                        }
                    }).show();
        }

    }

    /**获取未支付订单
     *
     */
    private static  void getUnPaidOrder(final Activity activity){

        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.stringOut(SPConstant.USER_ID));

        Observable observable = ApiFactory.getEducationApiSingleton().getHomePageInfo(SPUtil.stringOut(SPConstant.TOKEN), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<HomePageResult>(activity) {
            @Override
            protected void _onNext(final HomePageResult result) {
                if (null != result.getUnpaidOrderInfo()){
                    PayActivity.startPayActivity(activity , result.getUnpaidOrderInfo().getOrderId());
                }
            }

            @Override
            protected void _onError(String message) {
                MyToast.show(activity , message);
            }
        } , true);

    }
}
